﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace XmlConfig
{
    public class XmlConfigurator
    {
        const string XML_CONFIG_FILE = "config.xml";

        XmlDocument xmlConfig;

        public XmlConfigurator()
        {
            xmlConfig = new XmlDocument();
            xmlConfig.Load(XML_CONFIG_FILE);
        }

        public void set(string tagName, string data)
        {
            XmlNodeList nodeList = xmlConfig.GetElementsByTagName(tagName);
            if (nodeList.Count < 1) return;
            XmlNode node = nodeList[0];
            node.InnerText = data;
        }

        public string get(string tagName, string defaultString)
        {
            XmlNodeList nodeList = xmlConfig.GetElementsByTagName(tagName);
            if (nodeList.Count < 1)
            {
                //Create a new node and add it to the document.
                //The text node is the content of the price element.
                XmlElement elem = xmlConfig.CreateElement(tagName);
                XmlText text = xmlConfig.CreateTextNode(defaultString);
                xmlConfig.DocumentElement.AppendChild(elem);
                xmlConfig.DocumentElement.LastChild.AppendChild(text);
                save();
                return defaultString;
            }
            XmlNode node = nodeList[0];
            return node.InnerText;
        }

        public string get(string tagName)
        {
            XmlNodeList nodeList = xmlConfig.GetElementsByTagName(tagName);
            if (nodeList.Count < 1)
            {
                XmlElement elem = xmlConfig.CreateElement(tagName);
                XmlText text = xmlConfig.CreateTextNode("");
                xmlConfig.DocumentElement.AppendChild(elem);
                xmlConfig.DocumentElement.LastChild.AppendChild(text);
                save();
                return "";
            }
                XmlNode node = nodeList[0];
            return node.InnerText;
        }

        public string[] getMany(string tagName)
        {
            XmlNodeList nodeList = xmlConfig.GetElementsByTagName(tagName);
            string[] sArray = new string[nodeList.Count];
            for (int i = 0; i < nodeList.Count; i++)
                sArray[i] = nodeList[i].InnerText;
            return sArray;
        }

        public int getInt(string tagName, int defaultInt)
        {
            XmlNodeList nodeList = xmlConfig.GetElementsByTagName(tagName);
            if (nodeList.Count < 1)
            {
                XmlElement elem = xmlConfig.CreateElement(tagName);
                XmlText text = xmlConfig.CreateTextNode(defaultInt.ToString());
                xmlConfig.DocumentElement.AppendChild(elem);
                xmlConfig.DocumentElement.LastChild.AppendChild(text);
                save();
                return defaultInt;
            }
            XmlNode node = nodeList[0];
            int i;
            if (!int.TryParse(node.InnerText, out i))
                i = defaultInt;
            return i;
         }

        public void setInt(string tagName, int data)
        {
            XmlNodeList nodeList = xmlConfig.GetElementsByTagName(tagName);
            if (nodeList.Count < 1) return;
            XmlNode node = nodeList[0];
            node.InnerText = data.ToString();
        }

        public void save()
        {
            xmlConfig.Save(XML_CONFIG_FILE);
        }
    }
}
